﻿using System.Reflection;
using Abp.Modules;
using GN.Pay.Core;
using GN.Pay.TradeChannel.AliPay;
using GN.Pay.TradeChannel.WeiXin;
using GN.Pay.TradeChannel.Gyb;
using GN.Pay.TradeChannel.UnionPay;
using GN.Pay.TradeChannel.YJPay;
using Abp.AutoMapper;
using System.Configuration;
using GN.Pay.Core.Services;
using GN.Pay.EntityFramework;
using GN.Pay.Core.Startup;
using Abp.Dependency;
using GN.Pay.Core.Repositories;
using Abp.Domain.Uow;
using GN.Pay.Application.Service;

namespace GN.Pay.Application
{
    [DependsOn(typeof(CoreModule), typeof(DataModule), typeof(AbpAutoMapperModule))]
    public class ApplicationModule : AbpModule
    {
        public override void PreInitialize()
        {
            base.PreInitialize();
            IocManager.Register<IBankCardQueryService, JuheBankCardQueryService>(DependencyLifeStyle.Singleton);
            IocManager.Register<IPaySmsService, JuhePaySmsService>(DependencyLifeStyle.Singleton);
        }

        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());

            var unitOfWorkManager = IocManager.Resolve<IUnitOfWorkManager>();           

            using (var unit = unitOfWorkManager.Begin(System.Transactions.TransactionScopeOption.RequiresNew))
            {
                var tradeChannelRepository = IocManager.Resolve<ITradeChannelRepository>();
                var configService = IocManager.Resolve<ISystemConfigureService>();

                TradeChannelInstaller.Instance.Register(tradeChannelRepository, typeof(AliPayServiceBase).Assembly);
                TradeChannelInstaller.Instance.Register(tradeChannelRepository, typeof(WeiXinServiceBase).Assembly);
                TradeChannelInstaller.Instance.Register(tradeChannelRepository, typeof(GybServiceBase).Assembly);
                TradeChannelInstaller.Instance.Register(tradeChannelRepository, typeof(UnionPayServiceBase).Assembly);
                TradeChannelInstaller.Instance.Register(tradeChannelRepository, typeof(YJPayServiceBase).Assembly);
               
                TradeChannel.Consts.NotifyRootUrl = configService.GetConfigureData("NotifyRootUrl");
                Consts.Start.MessageSign = configService.GetOrAddMessageSign();

                unit.Complete();
            }

        }
    }
}
